package cn.virens.web.controller.open.oauth;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.virens.web.common.BaseJSONController;
import cn.virens.web.components.open.oauth.Oauth2Service;

@Controller
@RequestMapping("open/oauth/{type}")
public class OauthController extends BaseJSONController {
	private @Autowired Oauth2Service mOauth2Service;

	@ModelAttribute
	public void init(@PathVariable("type") String type) {
		this.mOauth2Service.switchover(type);
	}

	@RequestMapping("authorize")
	public String authorize(HttpServletRequest request) throws Exception {
		return redirect(mOauth2Service.authorize(null));
	}

	private static String redirect(String redirectURI) {
		return "redirect:" + redirectURI;
	}
}
